$(document).ready(function(){
	InputLength('#meta_url','#count_meta_url');
	InputLength('#meta_title','#count_meta_title');
	InputLength('#meta_keywords','#count_meta_keywords');
	InputLength('#meta_description','#count_meta_description');

	if($("#f-admin-panel").length) {
		$('#conteiner_body').css({'margin-top':'35px'});
	}
	// МЕТА-теги
	$('#f-admin-panel-menu-meta').live('click',function(){
		if($(this).hasClass('active')) {
			$(this).removeClass('active');
			$('.f-admin-panel-menu-meta').slideToggle(500);
		} else {
			$(this).addClass('active');
			$('.f-admin-panel-menu-meta').slideToggle(500);
		}
		return false;
	});
	// Редактировать
	$('#f-admin-panel-menu-edit').live('click',function(){
		if($(this).hasClass('active')){
			$('*[eid]').removeClass('e-editable');
			$(this).removeClass('active');
		} else {
			$('*[eid]').addClass('e-editable');
			$(this).addClass('active');
		}
		return false;
	});
	$('.e-editable').live('click', function(e){
    	var crnt = $(this),
    		offset = crnt.position(),
    		type = crnt.attr('etype'),
    		field = '';
    	$('.e-editPopup').remove();
    	switch (type) {
		case 'textarea':
			field = '<textarea id="eTextArea"></textarea><a href="#" class="e-saveEdit textareaType"></a>';
			break;
		case 'img':
			field = '<input type="text" /><a href="#" class="e-saveEdit"></a><a id="eFileBrowse" href="/admin/site/browse" class="btn" >browse</a>';
			break;
		case 'text':
		default:
			field = '<input type="text" /><a href="#" class="e-saveEdit"></a>';
			break;
		}
    	crnt.parent().siblings().removeAttr('style');
    	crnt.parent().css({'z-index': parseInt(crnt.parent().css('z-index'))+1});
   	 	crnt.after('<div class="e-editPopup">'+field+'</div>');
   	 	$('.e-editPopup').css({'left':offset.left, 'top':offset.top-5, 'width':crnt.width()-12});
   	 	$('.e-editPopup input, .e-editPopup textarea').css({'width':crnt.width()-50});
   	 	$('.e-editPopup input, .e-editPopup textarea').val(crnt.html());
   	 	if(type=='textarea'){
   	 		var ckEditor = CKEDITOR.replace( 'eTextArea',{'language':'ru','toolbar':'Full','font_names':'Arial/Arial, Helvetica, sans-serif;Comic Sans MS/Comic Sans MS, cursive;Courier New/Courier New, Courier, monospace;Georgia/Georgia, serif;Lucida Sans Unicode/Lucida Sans Unicode, Lucida Grande, sans-serif;Tahoma/Tahoma, Geneva, sans-serif;Times New Roman/Times New Roman, Times, serif;Trebuchet MS/Trebuchet MS, Helvetica, sans-serif;Verdana/Verdana, Geneva, sans-serif;','fontSize_sizes':'8/8px;9/9px;10/10px;11/11px;12/12px;14/14px;16/16px;18/18px;20/20px;22/22px;24/24px;26/26px;28/28px;36/36px;48/48px;72/72px;','extraPlugins':'','skin':'moono','theme':'default','filebrowserBrowseUrl':'/admin/site/browse'});
   	 		ckEditor.on("instanceReady",function() {
   	 			$('#cke_eTextArea').css({'width':crnt.width()-50, 'float':'left'});
   	 		});
   	 	}
   	 	if(type=='img'){
   	 		$('.e-editPopup input').val(crnt.find('img').attr('src'));
	   	 	$('#eFileBrowse').live('click', function(){
		   	 	$.ajax({
		    		type: 'POST',
		    		url: $(this).attr('href'),
					success: function(data){
						$(data).appendTo('body');
				   }
				});
	   			return false;
	   		});
   	 	}   	 	
   	 	return false;
	});
    
    $('.e-saveEdit').live('click', function(){
    	var crnt = $(this),
    		p = crnt.parent(),
    		el = p.prev(),
    		v = crnt.prev().val();
    	if(crnt.hasClass('textareaType')) v = CKEDITOR.instances['eTextArea'].getData();
    	p.prev().html(v);
    	$('.e-editPopup input, .e-editPopup textarea').attr('disabled', 'disabled');
    	$.ajax({
    		type: 'POST',
    		url: '/site/eSaveEdit',
			data: {'val':v, 'ename': el.attr('ename'), 'etype': el.attr('etype'), 'etable': el.attr('etable'), 'eid': el.attr('eid')},
			success: function(data){
				$('.e-editPopup').html('Успешно сохранено!').addClass('e-notice');
		    	setTimeout("$('.e-editPopup.e-notice').fadeOut('slow')", 2000);
		   }
		});
    	return false;
    });
});

// function InputLength(findInput, lengthInput){
function InputLength(findInput, lengthInput){
	
		var curLength = $(findInput).val().length;
		$(lengthInput).text(curLength);

    // var maxLength = $('#textarea').attr('maxlength');        //(1)
    $(findInput).live('keyup',function()
    {
		var curLength = $(this).val().length;
		$(lengthInput).text(curLength);
		/*$(this).val($(this).val().substr(0, maxLength));     //max length
		var remaning = maxLength - curLength;
		if (remaning < 0) remaning = 0;
		$('#textareaFeedback').html(remaning + ' осталось символов');
		if (remaning < 10)          //(5)
		{
		    $('#textareaFeedback').addClass('warning')
		}
		else
		{
		    $('#textareaFeedback').removeClass('warning')
		}*/
    })
}